package com.aktk.battlenole;

import java.util.ArrayList;
import java.util.Random;

import com.aktk.gameelements.GamePlay;
import com.aktk.gameelements.Player;

public class GameData {
	
	public static Player p1;
	public static Player p2;
	public static Stages stage;
	public static int numDevices;
	public static int playerTurn;
	public static LastResult lr;
	public static int shotCount;
	public static int PlayerID;
	public static int gameID;
	public static int loopCounter;
	
	public enum LastResult {
		 HIT, MISS, NULL;
	}
	
	public enum Stages {
		 MAIN_PAGE , PLAYER1_SETUP, PLAYER2_SETUP, SWITCH_PLAYER, GAME;
	}

	public GameData() {
		// TODO Auto-generated constructor stub
	}
	
	public static void setPlayer( int playerNumber , Player p ) {
		if( playerNumber == 1 )
		{
			p1 = p;
		}
		if( playerNumber == 2 )
		{
			p2 = p;
		}
    }
	
	public static Player getPlayer( int playerNumber ) {
		if( playerNumber == 1 )
		{
			return p1;
		}
		if( playerNumber == 2 )
		{
			return p2;
		}
		
		return null;
    }
	
	public static void startGame(){
		p1.opponent = p2;
		p2.opponent = p1;
		lr = LastResult.NULL;
		
		Random rand = new Random();
		playerTurn = 1 + rand.nextInt(2);
	}
	
	public static void nextTurn(){
		playerTurn = 2 - ( ( playerTurn + 1 ) % 2 );
	}
	
	public static Player getCurrentPlayer(){
		if( playerTurn == 1 )
		{
			return p1;
		}
		return p2;
	}
	
	public static Player getCurrentOpponent(){
		if( playerTurn == 1 )
		{
			return p2;
		}
		return p1;
	}
	
	public static void setCurrentPlayer( Player p ){
		if( playerTurn == 1 )
		{
			p1 = p;
		}
		else
		{
			p2 = p;
		}
	}
	
	public static void setCurrentOpponent( Player p ){
		if( playerTurn == 1 )
		{
			p2 = p;
		}
		else
		{
			p1 = p;
		}
	}
	
	public static boolean isGameOver(){
		if( p1.getShipsRemaining() == 0 )
		{
			return true;
		}
		if( p2.getShipsRemaining() == 0 )
		{
			return true;
		}
		return false;
	}
	
	public static String winner(){
		if( !isGameOver() )
		{
			return "Game Not Over";
		}
		else if( p1.getShipsRemaining() > 0 )
		{
			return p1.name;
		}
		else if( p2.getShipsRemaining() > 0 )
		{
			return p2.name;
		}
		return "Error";
	}
	
	public static LastResult getLastResult(){
		return lr;
	}
	
}
